<?php

require_once 'Base.php';

class GoodscategoryModel extends Base {
	
	public function getList($where=[], $page= 0, $pagesize= 10){
		
		$this->_db = $this->getAdapter();
    	$select = $this->_db->select();
    	$select->from('shop_goods_category');
    	if($where){
			foreach ($where as $key => $val) {
				if (is_int($key)) {
				   
				   $select->where($val);
				} else {
				  
					$select->where($key, $val);
				}
			} 
		}
    	if($pagesize) $select->limitPage($page, $pagesize);
    	return $this->_db->fetchAll($select);
	}
	
	public function getListByIds($ids = []){
		
		$data = $this->getList(['catid in (?)' => $ids],0,0);	
		
		$list = [];
		foreach($data as $val){
			$list[$val['catid']] = $val;
		}
		
		return $list;
	}
	
	public function getCount($where=[]){
		//$sql = "SELECT count(*) FROM shop_goods_category where 1=1";
		$this->_db = $this->getAdapter();
    	$select = $this->_db->select();
		
		$select->from('shop_goods_category',"count(*) as total");
		 foreach ($where as $key => $val) {
            if (is_int($key)) {
               
               $select->where($val);
            } else {
              
                $select->where($key, $val);
            }
	     }   

		return $this->_db->fetchRow($select);
		
	}
	
	public function getCategory($where = []){
	
		$this->_db = $this->getAdapter();
    	$select = $this->_db->select();
    	$select->from('shop_goods_category');
    	
	    foreach ($where as $key => $val) {
            if (is_int($key)) {
               
               $select->where($val);
            } else {
              
                $select->where($key, $val);
            }
	    }   
		
		return $this->_db->fetchRow($select);

	}
	
	public function save($data = [], $where = []){
		
		$res=$this->_db->update('shop_goods_category',$data,$where);
		return $res;
		
	}
	
	public function add($data = []){
		
		$this->_db->insert('shop_goods_category',$data);
		return  $this->_db->lastInsertId();
		
	}
	
	public function delete($where = []){
		return $this->_db->delete('shop_goods_category',$where);
	}
	
	
}
